// pages/xq/index.js
const app = getApp()
import {
  urls
} from '../../api/urls'
import {
  broadcast_read,
  share_by_property,
  publish_appendix_list
} from '../../api/request'

Page({
  data: {
    font_size: 'show_size_m', //字号大、中、小
    broadcast_id: '',
    info: {},
    publish_appendix_lists: [],
    property_code: '',
  },

  onLoad: function (options) {

    if (options.cur_property_code) { //研判从分享链接过来
      app.globalData.cur_property_code = options.cur_property_code
      wx.setStorageSync('switch_code', options.cur_property_code)
      if (options.from=='friend_circle') {
//发现从朋友圈转过来 直接进入小程序主页
        wx.switchTab({
          url: '/pages/home/index',
          success(res) {
            let page = getCurrentPages().pop();
            if (page == undefined || page == null) {
              return
            }
            page.onLoad(); //重新刷新主页面
            return
          }
        })
      }
    }

    this.setData({
      broadcast_id: options.broadcast_id,
      property_code: options.property_code
    })
    this.share_by_property(this.data.property_code)
  },

  //小程序分享信息查询
  share_by_property(property_code) {
    var data = {
      para: [property_code]
    }
    share_by_property(data).then(res => {
      app.globalData.share_title = res.data[0].SHARE_TITLE
      app.globalData.share_img_url = res.data[0].SHARE_IMG_URL
      app.globalData.signature_name=wx.getStorageSync('signature_name'),

      this.user_log_record(app.globalData.cur_my_openid, app.globalData.cur_my_name, '', '', app.globalData.cur_property_code,
        app.globalData.cur_property_name, '1', '1', '1')
    })
  },



  onReady: function () {},

  onShow: function () {
    this.broadcast_read(this.data.broadcast_id)
    this.publish_appendix_list(this.data.broadcast_id)
  },

  //获取信息列表
  broadcast_read(broadcast_id) {
    var data = {
      para: '["' + broadcast_id + '"]',
    }
    broadcast_read(data).then(res => {
      this.setData({
        info: res.data[0]
      })


      //格式替换
      var article_content = this.data.info
      article_content.CONTENT = article_content.CONTENT.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block"')
      article_content.CONTENT = article_content.CONTENT.replace(/<section/g, '<div')
      article_content.CONTENT = article_content.CONTENT.replace(/\/section>/g, '\div>');
      //      article_content.CONTENT = article_content.CONTENT.replace(/\n/g, '<br>');

      this.setData({
        info: article_content,
        font_size: wx.getStorageSync('font_size')
      })

    })
  },
  //
  publish_appendix_list(broadcast_id) {
    var data = {
      para: '["' + broadcast_id + '"]',
    }
    publish_appendix_list(data).then(res => {
      this.setData({
        publish_appendix_lists: res.data
      })
    })
  },

  download_file(e) {
    var m_url = e.currentTarget.dataset.url;
    wx.setClipboardData({
      data: m_url,
      success: function (res) {
        // self.setData({copyTip:true}),
        wx.showModal({
          title: '附件链接复制成功',
          content: '请粘贴到游览器中打开或粘贴到微信好友窗口电脑打开',
        })
      }
    })
  },

  //字体控制
  font_show(e) {
    var m_size = e.currentTarget.dataset.size;
    this.setData({
      font_size: 'show_size_' + m_size
    })
    wx.setStorageSync('font_size', this.data.font_size)
  },

  //分享朋友圈
  onShareTimeline: function () {
    return {
      title: app.globalData.signature_name + this.data.info.TITLE,
      query: 'broadcast_id=' + this.data.broadcast_id + '&cur_property_code=' + this.data.property_code+'&from=friend_circle',
      imageUrl: 'https://yz.2alive.cn/image/logo.png',
    }
  },

  // 用户点击右上sha角分享
  onShareAppMessage: function () {
    return {
      title: app.globalData.signature_name + this.data.info.TITLE,
      path: '/pages/xq/index?broadcast_id=' + this.data.broadcast_id + '&cur_property_code=' + this.data.property_code+'&from=wechar_group',
      imageUrl: app.globalData.share_img_url,
    }
  },

})